﻿namespace FFF.Test.DataAccess.Framework
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Diagnostics;
    using Microsoft.EntityFrameworkCore.Metadata;
    using FFF.Test.DataAccess.Framework.Configuration.Dto;
    using FFF.Test.Util.Consts;
    using Z.EntityFramework.Extensions;

    /// <summary>
    /// 事务拦截 
    /// </summary>
    public class EFTransactionInterceptor : DbTransactionInterceptor
    {
        /// <summary>
        /// 提交后
        /// </summary>
        /// <param name="transaction">事务对象</param>
        /// <param name="interceptionContext">执行上下文</param>
        public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            base.Committed(transaction, interceptionContext);
            this.LogInfo("EFTransactionInterceptor.Committed", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="transaction">十五</param>
        /// <param name="interceptionContext">执行上下文</param>
        public override void Disposed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            base.Disposed(transaction, interceptionContext);
            this.LogInfo("EFTransactionInterceptor.Disposed", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="transaction">事务对象</param>
        /// <param name="interceptionContext">执行context</param>
        /// <param name="exception">异常信息</param>
        public override void Error(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext, Exception exception)
        {
            base.Error(transaction, interceptionContext, exception);
            this.LogInfo("EFTransactionInterceptor.Error", interceptionContext.EventData.ToString(), exception.Message);
        }

        /// <summary>
        /// 回滚事件
        /// </summary>
        /// <param name="transaction">事务对象</param>
        /// <param name="interceptionContext">context</param>
        public override void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            base.RolledBack(transaction, interceptionContext);
            this.LogInfo("EFTransactionInterceptor.RolledBack", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 事务开始
        /// </summary>
        /// <param name="transaction">事务</param>
        /// <param name="interceptionContext">context上下文</param>
        public override void Started(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            base.Started(transaction, interceptionContext);
            this.LogInfo("EFTransactionInterceptor.Started", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 使用触发
        /// </summary>
        /// <param name="transaction">事务对象</param>
        /// <param name="interceptionContext">执行上下文</param>
        public override void Used(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            base.Used(transaction, interceptionContext);
            this.LogInfo("EFTransactionInterceptor.Used", interceptionContext.EventData.ToString());
        }

        private void LogInfo(string method, string data)
        {
            Console.WriteLine("Intercepted on: {0}: \n\t{1}", method, data);
        }

        private void LogInfo(string method, string data, string exception)
        {
            Console.WriteLine("Intercepted on: {0}: \n\t{1} \n\t{2}", method, data, exception);
        }
    }

}
